﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace TierService
{
    public class DateHelper
    {
        /// <summary>
        /// 
        /// </summary>
        public DateHelper()
        {

        }
        #region 日期处理
        /// <summary>
        /// 格式化日期为2006-12-22
        /// </summary>
        /// <param name="dTime"></param>
        /// <returns></returns>
        public static string formatDate(DateTime dTime)
        {
            string rStr;
            rStr = dTime.ToString("yyyy-MM-dd");
            return rStr;
        }

        /// <summary>
        /// 获得一个日期是星期几（汉化）
        /// </summary>
        /// <param name="sDate"></param>
        /// <returns></returns>
        public static string getWeek(DateTime sDate)
        {
            string rStr = "";
            switch (sDate.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    rStr = "星期日";
                    break;
                case DayOfWeek.Monday:
                    rStr = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    rStr = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    rStr = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    rStr = "星期四";
                    break;
                case DayOfWeek.Friday:
                    rStr = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    rStr = "星期六";
                    break;
            }
            return rStr;
        }
        /// <summary>根据日期，获得星期几</summary>
        /// <param name="y">年</param> 
        /// <param name="m">月</param> 
        /// <param name="d">日</param> 
        /// <returns>星期几，1代表星期一；7代表星期日</returns>
        public static int getWeekDay(int y, int m, int d)
        {
            if (m == 1) m = 13;
            if (m == 2) m = 14;
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
            return week;
        }
        /// <summary>
        /// 取得当前日期是今年的第几周
        /// </summary>
        /// <returns></returns>
        public static int WeekOfTheYear()
        {
            //获取
            CultureInfo myCI = new CultureInfo("zh-CN");
            System.Globalization.Calendar myCal = myCI.Calendar;
            //日期格式
            CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
            DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;
            int thisWeek = myCal.GetWeekOfYear(DateTime.Now, myCWR, myFirstDOW);
            return thisWeek;
        }
        #endregion
    }
}
